# -*- coding:utf-8 -*-
import datetime
import os
import threading
import time

import wx
import wx.adv
from common.base import *
from common.dm import Dm
from tasks.bangpai_qiandao import guild_sign_in
from tasks.lingshuang import get_double


class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='猪儿虫助手', size=(820, 610), name='frame', style=541072896)

        # 添加菜单栏
        self.menubar = wx.MenuBar()

        # 创建菜单
        self.file_menu = wx.Menu()
        self.file_menu.Append(wx.ID_OPEN, "打开配置")
        self.file_menu.Append(wx.ID_SAVE, "保存配置")
        self.file_menu.AppendSeparator()
        self.file_menu.Append(wx.ID_EXIT, "退出")

        # 创建关于菜单
        self.help_menu = wx.Menu()
        self.help_menu.Append(wx.HELP, "使用说明")
        self.help_menu.Append(wx.ID_ABOUT, "帮助")

        # 将菜单添加到菜单栏
        self.menubar.Append(self.file_menu, "菜单")
        self.menubar.Append(self.help_menu, "关于")

        # 设置菜单栏
        self.SetMenuBar(self.menubar)

        # 绑定菜单事件
        #self.Bind(wx.EVT_MENU, self.on_open, id=wx.ID_OPEN)
        self.Bind(wx.EVT_MENU, self.on_save, id=wx.ID_SAVE)
        self.Bind(wx.EVT_MENU, self.on_exit, id=wx.ID_EXIT)
        self.Bind(wx.EVT_MENU, self.on_usage, id=wx.HELP)
        self.Bind(wx.EVT_MENU, self.on_about, id=wx.ID_ABOUT)

        self.启动窗口 = wx.Panel(self)
        self.Centre()
        self.Bind(wx.EVT_CLOSE, self.启动窗口_将被关闭)
        self.账号信息 = wx.ListCtrl(self.启动窗口, size=(770, 240), pos=(20, 20), name='账号信息', style=8227)
        self.账号信息.AppendColumn('序号', 2, 40)
        self.账号信息.AppendColumn('标题', 0, 210)
        self.账号信息.AppendColumn('句柄', 2, 90)
        self.账号信息.AppendColumn('角色', 2, 90)
        self.账号信息.AppendColumn('当前任务', 2, 90)
        self.账号信息.AppendColumn('任务状态', 2, 90)
        self.账号信息.AppendColumn('尺寸 宽x高', 2, 120)
        self.账号信息.AppendColumn('次数', 2, 40)
        self.账号信息.EnableCheckBoxes(True)
        账号信息_字体 = wx.Font(9, 72, 90, 700, False, 'Times New Roman', 33)
        self.账号信息.SetFont(账号信息_字体)
        self.账号信息.SetForegroundColour((0, 0, 255, 255))
        self.账号信息.SetOwnBackgroundColour((192, 192, 192, 255))
        self.启动程序 = wx.Button(self.启动窗口, size=(60, 40), pos=(20, 280), label='启动程序', name='button')
        self.启动程序.Bind(wx.EVT_BUTTON, self.启动程序_按钮被单击)
        self.是否自动登录 = wx.RadioButton(self.启动窗口, size=(104, 24), pos=(98, 287), name='radioButton', label='是否自动登录')
        self.是否自动登录.SetValue(True)
        self.是否自动登录.Bind(wx.EVT_LEFT_DOWN, self.是否自动登录_单选框_鼠标左键按下)

        ######################
        # 创建加粗字体对象
        bold_font = wx.Font(9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
        self.刷新 = wx.Button(self.启动窗口, size=(60, 40), pos=(20, 330), label='刷新', name='button')
        self.刷新.Bind(wx.EVT_BUTTON, self.刷新_按钮被单击)
        self.排列窗口 = wx.Button(self.启动窗口, size=(60, 40), pos=(100, 330), label='排列窗口', name='button')
        self.排列窗口.Bind(wx.EVT_BUTTON, self.排列窗口_按钮被单击)
        self.全选设备 = wx.Button(self.启动窗口, size=(60, 40), pos=(20, 380), label='全选', name='button')
        self.全选设备.Bind(wx.EVT_BUTTON, self.全选设备_按钮被单击)
        self.反选设备 = wx.Button(self.启动窗口, size=(60, 40), pos=(100, 380), label='反选', name='button')
        self.反选设备.Bind(wx.EVT_BUTTON, self.反选设备_按钮被单击)
        self.全部开始 = wx.Button(self.启动窗口, size=(60, 40), pos=(20, 440), label='全部开始', name='button')
        self.全部开始.SetFont(bold_font)  # 设置加粗字体
        self.全部开始.Bind(wx.EVT_BUTTON, self.全部开始_按钮被单击)
        self.任务结束 = wx.Button(self.启动窗口, size=(60, 40), pos=(100, 440), label='任务结束', name='button')
        self.任务结束.SetFont(bold_font)  # 设置加粗字体
        self.任务结束.Bind(wx.EVT_BUTTON, self.任务结束_按钮被单击)
        self.全部关闭 = wx.Button(self.启动窗口, size=(140, 40), pos=(20, 490), label='全部关闭', name='button')
        self.全部关闭.SetFont(bold_font)  # 设置加粗字体
        self.全部关闭.Bind(wx.EVT_BUTTON, self.全部关闭_按钮被单击)
        ##########################################################################################################
        self.可选任务 = wx.ListBox(self.启动窗口,
                               size=(90, 240), pos=(220, 300),
                               name='listBox',
                               choices=['领双经验', '帮派签到', '找宝图', '挖宝图', '秘境降妖', '三界情缘', '普通运镖', '科举乡试'],
                               style=32)
        self.执行任务 = wx.ListBox(self.启动窗口, size=(90, 240), pos=(420, 300), name='listBox', choices=[], style=32)
        self.添加 = wx.Button(self.启动窗口, size=(40, 20), pos=(340, 360), label='->', name='button')
        self.添加.Bind(wx.EVT_BUTTON, self.添加_按钮被单击)
        self.删除 = wx.Button(self.启动窗口, size=(40, 20), pos=(340, 390), label='<--', name='button')
        self.删除.Bind(wx.EVT_BUTTON, self.删除_按钮被单击)
        self.清空 = wx.Button(self.启动窗口, size=(40, 30), pos=(340, 460), label='清空', name='button')
        self.清空.Bind(wx.EVT_BUTTON, self.清空_按钮被单击)
        self.保存任务 = wx.Button(self.启动窗口, size=(40, 30), pos=(340, 500), label='保存', name='button')
        self.保存任务.Bind(wx.EVT_BUTTON, self.保存任务_按钮被单击)

        self.可选任务标签 = wx.StaticText(self.启动窗口, size=(120, 20), pos=(190, 280), label='可选任务：', name='staticText',
                                    style=2321)
        self.执行任务标签 = wx.StaticText(self.启动窗口, size=(120, 20), pos=(390, 280), label='执行任务：', name='staticText',
                                    style=2321)

        ######################
        self.领双经验 = wx.Button(self.启动窗口, size=(56, 32), pos=(540, 340), label='领双经验', name='button')
        self.领双经验.Bind(wx.EVT_BUTTON, self.领双经验_按钮被单击)
        self.帮派签到 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66, 340), label='帮派签到', name='button')
        self.帮派签到.Bind(wx.EVT_BUTTON, self.帮派签到_按钮被单击)
        self.找宝图 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66 * 2, 340), label='找宝图', name='button')
        self.找宝图.Bind(wx.EVT_BUTTON, self.找宝图_按钮被单击)
        self.挖宝图 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66 * 3, 340), label='挖宝图', name='button')
        self.挖宝图.Bind(wx.EVT_BUTTON, self.挖宝图_按钮被单击)

        # 第二行
        self.秘境降妖 = wx.Button(self.启动窗口, size=(56, 32), pos=(540, 340 + 42), label='秘境降妖', name='button')
        self.秘境降妖.Bind(wx.EVT_BUTTON, self.秘境降妖_按钮被单击)
        self.三界情缘 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66, 340 + 42), label='三界情缘', name='button')
        self.三界情缘.Bind(wx.EVT_BUTTON, self.三界情缘_按钮被单击)
        self.答题 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66 * 2, 340 + 42), label='科举乡试', name='button')
        self.答题.Bind(wx.EVT_BUTTON, self.科举乡试_按钮被单击)
        self.竞技场 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66 * 3, 340 + 42), label='竞技场', name='button')
        self.竞技场.Bind(wx.EVT_BUTTON, self.竞技场_按钮被单击)

        # 第三行
        self.门派任务 = wx.Button(self.启动窗口, size=(56, 32), pos=(540, 340 + 42 * 2), label='门派任务', name='button')
        self.门派任务.Bind(wx.EVT_BUTTON, self.门派任务_按钮被单击)
        self.百草园 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66, 340 + 42 * 2), label='百草园', name='button')
        self.百草园.Bind(wx.EVT_BUTTON, self.百草园_按钮被单击)
        self.普通押镖 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66 * 2, 340 + 42 * 2), label='普通押镖', name='button')
        self.普通押镖.Bind(wx.EVT_BUTTON, self.普通押镖_按钮被单击)
        self.召唤乐园 = wx.Button(self.启动窗口, size=(56, 32), pos=(540 + 66 * 3, 340 + 42 * 2), label='召唤乐园', name='button')
        self.召唤乐园.Bind(wx.EVT_BUTTON, self.召唤乐园_按钮被单击)

        self.押镖任务 = wx.Button(self.启动窗口, size=(56 + 66, 32), pos=(540, 340 + 42 * 3), label='普通&高级押镖', name='button')
        self.押镖任务.Bind(wx.EVT_BUTTON, self.押镖任务_按钮被单击)
        ############################################

        self.捉鬼次数 = wx.StaticText(self.启动窗口, size=(60, 20), pos=(540, 294), label='捉鬼次数：', name='staticText',
                                  style=2321)
        self.次数 = wx.TextCtrl(self.启动窗口, size=(60, 25), pos=(540 + 60, 290), value='10', name='text', style=0)
        self.捉鬼任务 = wx.Button(self.启动窗口, size=(56 + 66, 32), pos=(540 + 66 * 2, 290), label='捉鬼任务', name='button')
        self.捉鬼任务.Bind(wx.EVT_BUTTON, self.捉鬼任务_按钮被单击)

        # 账号信息
        self.dev_all_info = []  # 设备列表 所有信息 含选中和未选中
        self.on_load()
        self.is_refreshing = False  # 添加标志位

    def 启动程序_按钮被单击(self, event):
        log.info('启动程序,按钮被单击')
        thread = threading.Thread(target=self.start_app_login, args=())
        thread.start()

    def 刷新_按钮被单击(self, event):
        log.info('刷新,按钮被单击')
        self.账号信息.DeleteAllItems()
        thread = threading.Thread(target=self.update_table, args=())
        thread.start()

    def 排列窗口_按钮被单击(self, event):
        # 根据窗口数 进行缩放 排列
        log.info('排列窗口,按钮被单击')
        thread = threading.Thread(target=move_app, args=(self.dev_all_hwnds,))
        thread.start()

    def 全部开始_按钮被单击(self, event):
        logging.info('全部开始,按钮被单击')
        thread = threading.Thread(target=self.start_all_task, args=())
        thread.start()

    def 任务结束_按钮被单击(self, event):
        logging.info('任务结束,按钮被单击')
        wx.MessageDialog(self, "暂不支持，请手动关闭 【助手】 来终止任务",
                         "提示", wx.OK | wx.ICON_INFORMATION).ShowModal()

    def 全部关闭_按钮被单击(self, event):
        logging.info('全部开始,按钮被单击')
        os.system("taskkill /IM MyGame_x64r.exe /F")

    def 全选设备_按钮被单击(self, event):
        logging.info('全选设备,按钮被单击')
        thread = threading.Thread(target=self.seclect_all_dev, args=())
        thread.start()

    def 反选设备_按钮被单击(self, event):
        logging.info('反选设备,按钮被单击')
        thread = threading.Thread(target=self.deselect_all_dev, args=())
        thread.start()

    def 添加_按钮被单击(self, event):
        logging.info('添加任务,按钮被单击')
        thread = threading.Thread(target=self.add_selection, args=(event,))
        thread.start()

    def 删除_按钮被单击(self, event):
        logging.info('删除任务,按钮被单击')
        thread = threading.Thread(target=self.dele_deselection, args=(event,))
        thread.start()

    def 清空_按钮被单击(self, event):
        logging.info('清空任务,按钮被单击')
        self.执行任务.Clear()

    def 是否自动登录_单选框_鼠标左键按下(self, event):
        logging.info('按钮11,按钮被单击')
        statu = self.是否自动登录.GetValue()
        if statu:
            self.是否自动登录.SetValue(False)
        else:
            self.是否自动登录.SetValue(True)

    def 保存任务_按钮被单击(self, event):
        logging.info('保存任务,按钮被单击')
        self.on_save(event)
        self.on_show_info()

    def 启动窗口_将被关闭(self, event):
        logging.info('启动窗口,将被关闭')
        wx.Exit()
        wx.Abort()

    def 领双经验_按钮被单击(self, event):
        logging.info('领双经验_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.领双经验_task, args=(i,))
                thread.start()

    def 帮派签到_按钮被单击(self, event):
        logging.info('帮派签到_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.帮派签到_task, args=(i,))
                thread.start()

    def 找宝图_按钮被单击(self, event):
        logging.info('找宝图_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.找宝图_task, args=(i,))
                thread.start()

    def 挖宝图_按钮被单击(self, event):
        logging.info('挖宝图_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.挖宝图_task, args=(i,))
                thread.start()

    def 秘境降妖_按钮被单击(self, event):
        logging.info('秘境降妖_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.秘境降妖_task, args=(i,))
                thread.start()

    def 捉鬼任务_按钮被单击(self, event):
        logging.info('捉鬼任务_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.捉鬼任务_task, args=(i,))
                thread.start()

    def 三界情缘_按钮被单击(self, event):
        logging.info('三界情缘_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.三界情缘_task, args=(i,))
                thread.start()

    def 科举乡试_按钮被单击(self, event):
        logging.info('科举乡试_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.科举乡试_task, args=(i,))
                thread.start()

    def 竞技场_按钮被单击(self, event):
        logging.info('竞技场_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.竞技场_task, args=(i,))
                thread.start()

    def 门派任务_按钮被单击(self, event):
        logging.info('门派任务_按钮被单击')

    def 百草园_按钮被单击(self, event):
        logging.info('百草园_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.百草园_task, args=(i,))
                thread.start()

    def 普通押镖_按钮被单击(self, event):
        logging.info('普通押镖_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.普通运镖_task, args=(i,))
                thread.start()

    def 召唤乐园_按钮被单击(self, event):
        logging.info('普通押镖_按钮被单击')
        for i in range(self.账号信息.GetItemCount()):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.召唤乐园_task, args=(i,))
                thread.start()

    def 押镖任务_按钮被单击(self, event):
        logging.info('押镖任务_按钮被单击')

    def on_save(self, event):
        # 获取 ListBox 中的数据
        data = self.执行任务.GetStrings()
        # 将数据保存到文件
        with open("./config/task_config.txt", 'w', encoding="utf-8") as file:
            for item in data:
                file.write(f"{item}\n")
        wx.LogDebug(f"执行任务配置保存 成功！")

    def on_load(self):
        # 从文件中读取数据并更新 ListBox
        try:
            with open("./config/task_config.txt", 'r', encoding="utf-8") as file:
                data = [line.strip() for line in file]
            self.执行任务.Set(data)
        except Exception as e:
            wx.LogError(f"Failed to load data: {e}")

    def on_show_info(self):
        wx.MessageDialog(self, "执行任务配置保存成功，保存文件为：\n./config/task_config.txt",
                         "提示", wx.OK | wx.ICON_INFORMATION).ShowModal()

    def on_exit(self, event):
        """退出应用程序"""
        self.Close()

    def on_usage(self, event):
        """显示使用说明对话框"""
        usage_text = """
        使用说明：
        【任务执行前，请完成签到（刮刮乐），并关闭游戏中弹窗】
        - 启动程序按钮：点击后启动相关程序。在config/start_config.txt中配置启动程序信息。
        - 是否自动登录：用于启动时，勾选后自动登录进入游戏；未勾选只会启动游戏，需用户自己选择角色并登录。
        - 刷新按钮：刷新账号信息表格中设备信息。
        - 全选按钮：勾选后，账号信息表格中所有设备都会被选中。
        - 反选按钮：勾选后，账号信息表格中所有设备都会被反选。
        - 全部开始：点击后，根据执行任务列表中的任务，依次执行。
        - 任务结束：【未实现】
        - 全部关闭：关闭全部游戏。
        - ->：选中可选任务，点击->会添加到执行任务列表中，可重复添加。
        - <-：选中执行任务列表中的任务，点击<-会从执行任务列表中移除。
        - 清空：将执行任务列表任务全部清除
        - 保存按钮：保存执行任务列表中的任务和捉鬼次数到文件，以便再次使用软件加载之前配置。
        【单任务】
        - 捉鬼任务：开始捉鬼前提：设置捉鬼次数和组队，队长需要在钟馗领取捉鬼任务（需存在捉鬼任务弹窗），尽量保证队伍稳定和血量充足。
        - 领双经验：领取双倍经验
        - 帮派签到：帮派签到（已加入帮派）
        - 找宝图：找寻宝图
        - 挖宝图：挖取宝图
        - 秘境降妖：执行秘境降妖任务
        - 三界情缘：自动适配两种方式：1.未打开三界情缘答题界面，2.已打开三界情缘答题界面
        - 科举乡试：自动适配两种方式：1.未打开科举乡试答题界面，2.已打开科举乡试答题界面
        - 竞技场：需要配置助战，并进入到竞技场匹配界面
        - 门派任务：需要手动组队，并勾选队长账号
        - 百草园：需要手动进入帮派，弹窗浇水、施肥、除虫界面。【注：不会自动采集草药】
        
        """
        dlg = wx.MessageDialog(self, usage_text, "使用说明", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    def on_about(self, event):
        """显示关于对话框"""
        info = wx.adv.AboutDialogInfo()
        info.SetName("猪儿虫助手")
        info.SetVersion("1.0")
        info.SetDescription("一个用于游戏自动化的辅助工具")
        info.SetCopyright("(C) 2025")
        info.AddDeveloper("开发者")
        wx.adv.AboutBox(info)

    ####################################################################################################################
    def start_app_login(self):
        # 启动游戏
        logging.info('start_app_login')
        exp_conter = start_app("./config/start_config.txt")
        self.dev_all_hwnds = get_hwnds_from_start(exp_conter)
        if self.是否自动登录.GetValue():
            for i in range(len(self.dev_all_hwnds)):
                login_app(self.dev_all_hwnds[i])

    def update_table(self):
        try:
            self.dev_all_hwnds = []
            self.dev_all_info = get_all_devs()
            for i in range(len(self.dev_all_info)):
                self.账号信息.Append(self.dev_all_info[i])
                self.dev_all_hwnds.append(self.dev_all_info[i][2])
        finally:
            self.is_refreshing = False  # 刷新完成，重置标志位

    def seclect_all_dev(self):
        logging.info("全选设备，已执行")
        for i in range(self.账号信息.GetItemCount()):
            self.账号信息.CheckItem(i, True)

    def deselect_all_dev(self):
        # 反选
        logging.info("反选设备，已执行")
        for i in range(self.账号信息.GetItemCount()):
            is_checked = self.账号信息.IsItemChecked(i)
            # 取反勾选状态
            self.账号信息.CheckItem(i, not is_checked)

    def update_data(self):
        logging.info('更新,账号信息 表格')
        self.账号信息.DeleteAllItems()
        for i in range(len(self.dev_all_info)):
            self.账号信息.Append(self.dev_all_info[i])
            self.账号信息.CheckItem(i, self.dev_all_info[i][6])

    def add_selection(self, event):
        # 列表框  获取选中的项
        selected_index = self.可选任务.GetSelection()
        selected_item = self.可选任务.GetString(selected_index)
        logging.info(f"Selected: {selected_item}")
        self.执行任务.Append(selected_item)
        return selected_item

    def dele_deselection(self, event):
        # 列表框  获取取消选中的项
        deselected_index = event.GetSelection()
        deselected_item = self.执行任务.GetString(deselected_index)
        logging.info(f"Deselected: {deselected_item}")
        self.执行任务.Delete(deselected_index)
        return deselected_index

    def start_all_task(self):
        logging.info('start_all_task')
        for i in range(len(self.dev_all_hwnds)):
            if self.账号信息.IsItemChecked(i):
                thread = threading.Thread(target=self.add_do_task, args=(i,))
                thread.start()

    def add_do_task(self, index):
        logging.info('add_do_task')
        for task in self.执行任务.GetStrings():
            if task == "帮派签到":
                self.帮派签到_task(index)
            if task == "领双经验":
                self.领双经验_task(index)
            if task == "三界情缘":
                self.三界情缘_task(index)
            if task == "科举乡试":
                self.科举乡试_task(index)
            if task == "普通运镖":
                self.普通押镖_task(index)
            if task == "挖宝图":
                self.挖宝图_task(index)
            if task == "找宝图":
                self.找宝图_task(index)
            if task == "秘境降妖":
                self.秘境降妖_task(index)

    def 帮派签到_task(self, index):
        logging.info(f'【{index}】帮派签到_task')
        self.账号信息.SetItem(index, 4, "帮派签到")
        self.账号信息.SetItem(index, 5, "进行中")
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        # 定义函数参数
        dm = Dm()
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 具体的帮派方法
        time.sleep(2)
        guild_sign_in(dm, hwnd)

        # 任务结束
        logging.info(f'【{index}】帮派签到_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 领双经验_task(self, index):
        logging.info(f'【{index}】领双经验_task')
        self.账号信息.SetItem(index, 4, "领双经验")
        self.账号信息.SetItem(index, 5, "进行中")
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        # 定义函数参数
        dm = Dm()
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 具体的帮派方法
        time.sleep(2)
        get_double(dm, hwnd)

        # 任务结束
        logging.info(f'【{index}】领双经验_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 三界情缘_task(self, index):
        logging.info(f'【{index}】三界情缘_task')
        self.账号信息.SetItem(index, 4, "三界情缘")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.sanjieqingyuan(dm, index)

        # 任务结束
        logging.info(f'【{index}】领双经验_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 科举乡试_task(self, index):
        logging.info(f'【{index}】科举乡试_task')
        self.账号信息.SetItem(index, 4, "科举乡试")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()

        # 具体的帮派方法
        time.sleep(2)
        self.kejuxiangshi(dm, index)

        # 任务结束
        logging.info(f'【{index}】科举乡试_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 普通运镖_task(self, index):
        logging.info(f'【{index}】普通运镖_task')
        self.账号信息.SetItem(index, 4, "普通运镖")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.putongyunbiao(dm, index)

        # 任务结束
        logging.info(f'【{index}】普通运镖_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 捉鬼任务_task(self, index):
        logging.info(f'【{index}】捉鬼任务_task')
        self.账号信息.SetItem(index, 4, "捉鬼任务")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.zhuogui(dm, index)

        # 任务结束
        logging.info(f'【{index}】捉鬼任务_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 挖宝图_task(self, index):
        logging.info(f'【{index}】挖宝图_task')
        self.账号信息.SetItem(index, 4, "挖宝图")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.wabaotu(dm, index)

        # 任务结束
        logging.info(f'【{index}】挖宝图_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 竞技场_task(self, index):
        logging.info(f'【{index}】竞技场_task')
        self.账号信息.SetItem(index, 4, "竞技场")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.jingjichang(dm, index)

        # 任务结束
        logging.info(f'【{index}】竞技场_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 百草园_task(self, index):
        logging.info(f'【{index}】药草_task')
        self.账号信息.SetItem(index, 4, "药草")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.yaochai(dm, index)

        # 任务结束
        logging.info(f'【{index}】药草_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 找宝图_task(self, index):
        logging.info(f'【{index}】宝图任务_task')
        self.账号信息.SetItem(index, 4, "找宝图")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.zhaobaotu(dm, index)

        # 任务结束
        logging.info(f'【{index}】宝图任务_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 秘境降妖_task(self, index):
        logging.info(f'【{index}】秘境降妖_task')
        self.账号信息.SetItem(index, 4, "秘境降妖")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.mijingxiangyao(dm, index)

        # 任务结束
        logging.info(f'【{index}】秘境降妖_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    def 召唤乐园_task(self, index):
        logging.info(f'【{index}】召唤乐园_task')
        self.账号信息.SetItem(index, 4, "召唤灵乐园")
        self.账号信息.SetItem(index, 5, "进行中")
        # 定义函数参数
        dm = Dm()
        # 具体的帮派方法
        time.sleep(2)
        self.zhaohuanleyuan(dm, index)

        # 任务结束
        logging.info(f'【{index}】召唤灵乐园_结束')
        self.账号信息.SetItem(index, 5, "已完成")
        dm.unbind_window()

    #############################################################
    # 具体任务
    def sanjieqingyuan(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")

        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)

        # 2.首页-三界情缘
        sleep(2)
        pic_path = dm.Capture(356, 125, 832, 173, f"test{number}.bmp")
        sleep(5)
        retx, rety = OCR_get_text(pic_path, "第|题")
        if retx == -1:
            log.info(f"设备{number}，未找到，三界情缘")
            sleep(5)
            self.select_task(dm, number, "三界")

        # 2.三界情缘 答题
        fale = True
        counter = 0
        while fale:
            print(f"设备{number}，正在答题")
            sleep(5)
            pic_path_ques = dm.Capture(344, 120, 917, 171, f"test_question_{number}.bmp")
            pic_path_answ = dm.Capture(346, 255, 930, 502, f"test_answer_{number}.bmp")
            keju_py = os.path.join(base_dir, "config", "sanjie_dati.py")
            sanjie_xml = os.path.join(base_dir, "config", "answer_sanjie.xls")
            pic_path = dm.Capture(344, 120, 917, 171, f"test{number}.bmp")
            sleep(2)
            retx, rety = OCR_get_text(pic_path, "第|题")
            if retx == -1 and counter > 10:
                log.info(f"设备{number}，未找到，题目")
                fale = False
            else:
                retx, rety = self.sanjieanswer(pic_path_ques, keju_py, pic_path_answ, sanjie_xml)
                if retx != -1:
                    dm.LeftClick(retx + 346, rety + 255 - 100, 10, 10)
                    sleep(2)
                else:
                    log.info(f"设备{number}，未找到，答案")
                    fale = False
            if counter > 30:
                fale = False
            counter += 1
            self.账号信息.SetItem(index, 7, str(counter))

        # 4 点击领取
        sleep(2)
        dm.LeftClick(231, 336, 10, 10)

        # 5.挂机-关闭
        for _ in range(5):
            sleep(5)
            x, y = dm.Find_pic(904, 125, 982, 195, "三界情缘-关闭X.bmp", "000000", 0.8, 0)
            if x != -1 and y != -1:
                dm.LeftClick(x, y, 5, 5)
                sleep(2)
            else:
                log.info(f"设备{number}，未找到，三界情缘-关闭X.bmp")

    def sanjieanswer(self, pic_path_ques, keju_py, pic_path_ans, sanjie_xml):
        cmd = [python64, keju_py, pic_path_ques, pic_path_ans, sanjie_xml]
        print(" ".join(cmd))
        x = -1
        y = -1
        try:
            # 使用 subprocess 模块执行命令并获取返回值
            result = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8')
            # 按行分割返回值
            lines = result.stdout.splitlines()
            # 筛选包含“坐标”的行
            for line in lines:
                if "坐标" in line:
                    xy = line.split("|")
                    x = int(float(xy[1]))
                    y = int(float(xy[2]))
                    return x, y
            return x, y
        except Exception as e:
            print(f"执行命令时出错: {e}")
            return x, y

    def kejuxiangshi(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)

        # 2.首页-科举乡试
        sleep(2)
        pic_path = dm.Capture(408, 113, 628, 195, f"test{number}.bmp")
        sleep(2)
        retx, rety = OCR_get_text(pic_path, "科|举|乡|试")
        if retx == -1:
            log.info(f"设备{number}，未找到，科举乡试")
            self.select_task(dm, number, "科举乡试")

        # 2.科举乡试 答题
        counter = 0
        start_time = time.time()  # 记录开始时间
        while counter < 11 and (time.time() - start_time) < 60:
            sleep(2)
            pic_path_ques = dm.Capture(339, 225, 936, 334, f"test_question_{number}.bmp")
            sleep(2)
            pic_path_answ = dm.Capture(339, 306, 942, 530, f"test_answer_{number}.bmp")
            sleep(2)
            keju_py = os.path.join(base_dir, "config", "keju_dati.py")
            keju_xml = os.path.join(base_dir, "config", "answer_keju.xls")
            retx, rety = self.kejuanswer(pic_path_ques, keju_py, pic_path_answ, keju_xml)
            if retx != -1:
                dm.LeftClick(retx + 339 + 30, rety + 306, 5, 5)
                sleep(5)
                counter += 1
                start_time = time.time()
            else:
                log.info(f"设备{number}，未找到，答案")
            self.账号信息.SetItem(index, 7, str(counter))
        # 5.挂机-关闭
        for _ in range(5):
            sleep(5)
            x, y = dm.Find_pic(923,180,987,240, "科举乡试-科举乡试-关闭.bmp", "000000", 0.8, 0)
            if x != -1 and y != -1:
                dm.LeftClick(x, y, 5, 5)
                sleep(2)
            else:
                log.info(f"设备{number}，未找到，科举乡试-科举乡试-关闭.bmp")

            x, y = dm.Find_pic(268, 5, 357, 81, "首页-活动.bmp", "000000", 0.8, 0)
            if x != -1 and y != -1:
                break

    def kejuanswer(self, pic_path_ques, keju_py, pic_path_ans, keju_xml):
        cmd = [python64, keju_py, pic_path_ques, pic_path_ans, keju_xml]
        log.info(" ".join(cmd))
        x = -1
        y = -1
        try:
            # 使用 subprocess 模块执行命令并获取返回值
            result = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8')
            # 按行分割返回值
            lines = result.stdout.splitlines()
            log.info(lines)
            # 筛选包含“坐标”的行
            for line in lines:
                if "坐标" in line:
                    xy = line.split("|")
                    x = int(float(xy[1]))
                    y = int(float(xy[2]))
                    return x, y
            return x, y
        except Exception as e:
            print(f"执行命令时出错: {e}")
            return -1, -1

    def select_task(self, dm, number, taskname):
        # 2.首页-活动
        fale = False
        for i in range(3):
            sleep(2)
            pic_path = dm.Capture(268, 5, 357, 81, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "活动")
            if retx != -1:
                dm.LeftClick(retx + 268, rety + 5, 20, 10)
                sleep(2)
                pic_path = dm.Capture(59, 137, 221, 267, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, "日常活动")
                if retx != -1:
                    log.info(f"设备{number}，找到，活动任务--日常活动")
                    fale = True
                    break
            else:
                log.info(f"设备{number}，未找到，首页--活动")
                x, y = dm.Find_pic(268, 5, 357, 81, "首页-活动.bmp", "000000", 0.8, 0)
                if x != -1 and y != -1:
                    dm.LeftClick(x, y, 10, 10)
                    sleep(2)
                    pic_path = dm.Capture(59, 137, 221, 267, f"test{number}.bmp")
                    retx, rety = OCR_get_text(pic_path, "日常活动")
                    if retx != -1:
                        log.info(f"设备{number}，找到，活动任务--日常活动")
                        fale = True
                        break
                    if i ==2:
                        log.info(f"设备{number}，未找到，首页-活动.bmp，点击坐标")
                        dm.LeftClick(307,31, 10, 10)
                        sleep(2)
                        fale = True
                else:
                    log.info(f"设备{number}，未找到，首页-活动.bmp")
        if fale:
            # 3.活动列表-任务
            # 先移动鼠标到指定位置并向上滚动到顶部
            dm.MoveToEx(557, 258, 10, 10)

            for _ in range(40):
                pic_path = dm.Capture(236, 142, 957, 441, f"test{number}.bmp")
                retx1, rety1 = OCR_get_text(pic_path, "参加")
                dm.WheelUp()
                sleep(1)
                pic_path = dm.Capture(236, 142, 957, 441, f"test{number}.bmp")
                retx2, rety2 = OCR_get_text(pic_path, "参加")
                if retx1 == retx2:
                    break
            down_fale = True # 鼠标向下滚动
            while True:
                pic_path = dm.Capture(236, 142, 957, 441, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, taskname)
                if retx != -1:
                    log.info(f"设备{number}，找到，活动任务--日常活动--{taskname}")
                    sleep(2)
                    pic_path = dm.Capture(retx + 236, rety + 142, retx + 236 + 340, rety + 142 + 80,
                                          f"test{number}.bmp")  # 340和80是任务名称与参加的间距
                    retx2, rety2 = OCR_get_text(pic_path, "参加")
                    if retx2 != -1:
                        dm.LeftClick(retx + 236 + retx2, rety + 142 + rety2, 10, 10)
                        break
                    # 检查是否找到“完成”控件
                    retx3, rety3 = OCR_get_text(pic_path, "完成")
                    if retx3 != -1:
                        log.info(f"设备{number}，找到，活动任务--日常活动--完成")
                        break
                else:
                    if down_fale:
                        # 向下滚动5次鼠标
                        for _ in range(5):
                            dm.MoveToEx(557, 258, 10, 10)
                            dm.WheelDown()
                            sleep(1)
                    else:
                        for _ in range(5):
                            dm.MoveToEx(557, 258, 10, 10)
                            dm.WheelUp()
                            sleep(1)
                    pic_path = dm.Capture(238,153,958,421, f"test{number}.bmp")
                    retx, rety = OCR_get_text(pic_path, "完成")
                    if retx != -1:
                        log.info(f"设备{number}，找到，活动任务--日常活动--完成")
                        down_fale = False

        # 4.活动任务列表--关闭
        sleep(2)
        x, y = dm.Find_pic(890, 56, 1001, 157, "日常活动-关闭X.bmp", "000000", 0.8, 0)
        if x != -1 and y != -1:
            dm.LeftClick(x, y, 10, 10)
            sleep(2)
            return False
        else:
            log.info(f"设备{number}，未找到，日常活动-关闭X.bmp")

    def putongyunbiao(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)
        self.select_task(dm, number, "运镖")

        # 2.首页-押镖
        counter = 0
        while counter < 3:
            pic_path = dm.Capture(268, 5, 357, 81, f"test{number}.bmp")
            retx1, rety1 = OCR_get_text(pic_path, "活动")
            sleep(5)
            pic_path = dm.Capture(723, 134, 983, 621, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "普|通")
            if retx != -1:
                dm.LeftClick(retx + 723, rety + 134, 20, 5)
                sleep(2)
                pic_path = dm.Capture(517, 376, 705, 486, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, "确定")
                if retx != -1:
                    dm.LeftClick(retx + 517, rety + 376, 10, 10)
                    sleep(50)
                    counter = counter + 1
            self.账号信息.SetItem(index, 7, str(counter))
            sleep(10)
            pic_path = dm.Capture(268, 5, 357, 81, f"test{number}.bmp")
            retx2, rety2 = OCR_get_text(pic_path, "活动")
            if retx1 != -1 and retx2 != -1:
                break

    def zhuogui(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)

        # 弹窗的捉鬼任务
        pic_path = dm.Capture(704, 129, 996, 585, f"test{number}.bmp")
        retx, rety = OCR_get_text(pic_path, "捉鬼任务")
        if retx != -1:
            dm.LeftClick(retx + 704, rety + 129, 20, 5)
            sleep(2)
        # 任务列表中 捉鬼任务
        pic_path = dm.Capture(810, 163, 1024, 610, f"test{number}.bmp")
        retx, rety = OCR_get_text(pic_path, "捉鬼")
        if retx != -1:
            dm.LeftClick(retx + 810, rety + 163, 20, 5)
            sleep(2)
            dm.LeftClick(retx + 810, rety + 163, 20, 5)
            sleep(2)

        exp_counter = self.次数.GetValue()
        counter = 0
        while counter < int(exp_counter):
            # 弹窗提示 第2次任务执行
            pic_path = dm.Capture(308, 294, 725, 483, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "轮鬼|继续捉鬼")
            if retx != -1:
                # 点击确定
                pic_path = dm.Capture(494, 310, 730, 511, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, "确定")
                if retx != -1:
                    dm.LeftClick(retx + 494, rety + 310, 20, 5)
                    sleep(5)
                    counter = counter + 1
                    # 弹窗的捉鬼任务
                    for i in range(5):
                        sleep(2)
                        pic_path = dm.Capture(704, 129, 996, 585, f"test{number}.bmp")
                        retx, rety = OCR_get_text(pic_path, "捉鬼任务")
                        if retx != -1:
                            dm.LeftClick(retx + 704, rety + 129, 20, 5)
                            sleep(2)
                            # 任务列表中 捉鬼任务
                            for i in range(5):
                                sleep(5)
                                pic_path = dm.Capture(810, 163, 1024, 610, f"test{number}.bmp")
                                retx, rety = OCR_get_text(pic_path, "捉鬼")
                                if retx != -1:
                                    dm.LeftClick(retx + 810, rety + 163, 20, 5)
                                    sleep(2)
                                    dm.LeftClick(retx + 810, rety + 163, 20, 5)
                                    sleep(2)
            self.账号信息.SetItem(index, 7, str(counter))
            sleep(10)

    def wabaotu(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)

        # 弹窗的 宝图界面 判断首页有无宝图
        fale = True
        retx, rety = dm.Find_pic(740, 418, 968, 700, "首页-宝图-图标.bmp", "000000", 0.8, 0)
        if retx != -1:
            pic_path = dm.Capture(740, 418, 968, 700, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "使用")
            if retx != -1:
                dm.LeftClick(retx + 740, rety + 418, 5, 5)
                sleep(2)
            fale = False

        # 进入包裹里面，进行找宝图
        if fale:
            pic_path = dm.Capture(947, 606, 1019, 692, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "包|裹")
            if retx != -1:
                dm.LeftClick(retx + 947, rety + 606, 5, 5)
                sleep(2)
                # 滑动顶部

                for _ in range(20):
                    dm.MoveToEx(700, 360, 40, 20)
                    dm.WheelUp()
                    sleep(0.5)
                for i in range(20):
                    retx, rety = dm.Find_pic(517, 220, 907, 536, "包裹-宝图-图标.bmp", "000000", 0.8, 0)
                    if retx != -1:
                        dm.LeftClick(retx, rety, 5, 5)
                        sleep(2)
                        pic_path = dm.Capture(321, 411, 477, 516, f"test{number}.bmp")
                        retx, rety = OCR_get_text(pic_path, "使用")
                        if retx != -1:
                            dm.LeftClick(retx + 321, rety + 411, 5, 5)
                            sleep(2)
                            break
                    else:
                        for _ in range(5):
                            dm.MoveToEx(700, 360, 40, 20)
                            dm.WheelDown()
                            sleep(0.5)
                # 处理未找到图标 关闭
                retx, rety = dm.Find_pic(885, 75, 959, 135, "包裹-关闭.bmp", "000000", 0.8, 0)
                if retx != -1:
                    dm.LeftClick(retx, rety, 5, 5)
                    sleep(2)
            else:
                log.info(f"设备：{number},没有找到首页-宝图图标")
        counter = 1
        start_time = time.time()  # 记录开始时间
        while counter < 20 and (time.time() - start_time) < 300:
            # 弹窗的捉鬼任务
            retx, rety = dm.Find_pic(740, 418, 968, 700, "首页-宝图-图标.bmp", "000000", 0.8, 0)
            if retx != -1:
                pic_path = dm.Capture(740, 418, 968, 700, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, "使|用")
                counter = counter + 1
                if retx != -1:
                    dm.LeftClick(retx + 740, rety + 418, 5, 5)
                    sleep(20)
                    start_time = time.time()  # 重新记录开始时间
            else:
                log.info(f"设备：{number},没有找到首页-宝图图标")
            self.账号信息.SetItem(index, 7, str(counter))
            sleep(10)

    def jingjichang(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)

        counter = 1
        start_time = time.time()  # 记录开始时间
        while counter < 11 and (time.time() - start_time) < 300:
            pic_path = dm.Capture(830, 566, 911, 633, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "开始|匹配")
            if retx != -1:
                dm.LeftClick(retx + 830, rety + 566, 5, 5)
                counter = counter + 1
                sleep(10)

            else:
                log.info(f"设备：{number},没有找到--开始|匹配")

            pic_path = dm.Capture(940, 685, 1021, 766, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "自动")
            if retx != -1:
                dm.LeftClick(retx + 940, rety + 685, 5, 5)
                sleep(10)

                start_time = time.time()  # 重新记录开始时间
            else:
                log.info(f"设备：{number},没有找到--自动")

            retx, rety = dm.Find_pic(835, 138, 949, 224, "战斗-变形-关闭X.bmp", "000000", 0.8, 0)
            if retx != -1:
                dm.LeftClick(retx, rety, 5, 5)
                sleep(2)
            self.账号信息.SetItem(index, 7, str(counter))
            sleep(10)

    def zhaohuanleyuan(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)

        counter = 1
        start_time = time.time()  # 记录开始时间
        while counter < 11 and (time.time() - start_time) < 300:
            pic_path = dm.Capture(734,663,1003,763, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "开始|匹配")
            if retx != -1:
                dm.LeftClick(retx + 734, rety + 663, 5, 5)
                counter = counter + 1
                sleep(10)
            else:
                log.info(f"设备：{number},没有找到--开始|匹配")

            pic_path = dm.Capture(940, 685, 1021, 766, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "自动")
            if retx != -1:
                dm.LeftClick(retx + 940, rety + 685, 5, 5)
                sleep(10)
                start_time = time.time()  # 重新记录开始时间
            else:
                log.info(f"设备：{number},没有找到--自动")

            pic_path = dm.Capture(378,565,683,694, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "确定")
            if retx != -1:
                dm.LeftClick(retx + 378, rety + 565, 5, 5)
                sleep(10)
                start_time = time.time()  # 重新记录开始时间
            else:
                log.info(f"设备：{number},没有找到--自动")

            pic_path = dm.Capture(374,478,652,570, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "点击|空白|继续")
            if retx != -1:
                dm.LeftClick(retx + 374, rety + 478, 5, 5)
                sleep(10)
                start_time = time.time()  # 重新记录开始时间
            else:
                log.info(f"设备：{number},没有找到--自动")

            retx, rety = dm.Find_pic(835, 138, 949, 224, "战斗-变形-关闭X.bmp", "000000", 0.8, 0)
            if retx != -1:
                dm.LeftClick(retx, rety, 5, 5)
                sleep(2)
            self.账号信息.SetItem(index, 7, str(counter))
            sleep(10)

    def yaochai(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)

        counter = 1
        while datetime.datetime.now().hour < 13:
            if counter % 3 == 1:
                pic_path = dm.Capture(700, 320, 1000, 600, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, "浇水")
                if retx != -1:
                    dm.LeftClick(retx + 700, rety + 320, 5, 5)
                    counter = counter + 1
                    sleep(10)
                else:
                    log.info(f"设备：{number},没有找到--浇水")
            elif counter % 3 == 2:
                pic_path = dm.Capture(700, 320, 1000, 600, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, "施肥")
                if retx != -1:
                    dm.LeftClick(retx + 700, rety + 320, 5, 5)
                    counter = counter + 1
                    sleep(10)
                else:
                    log.info(f"设备：{number},没有找到--施肥")
            else:
                pic_path = dm.Capture(700, 320, 1000, 600, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, "除虫")
                if retx != -1:
                    dm.LeftClick(retx + 700, rety + 320, 5, 5)
                    counter = counter + 1
                    sleep(10)
                else:
                    log.info(f"设备：{number},没有找到--除虫")
            self.账号信息.SetItem(index, 7, str(counter))
            sleep(10)

    def zhaobaotu(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)
        self.select_task(dm, number, "宝图任务")

        # 2.首页-宝图任务
        for _ in range(3):
            sleep(5)
            pic_path = dm.Capture(703, 329, 997, 584, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "听|听|无|妨")
            if retx != -1:
                dm.LeftClick(retx + 703, rety + 329, 20, 5)
                sleep(2)
                dm.LeftClick(retx + 703, rety + 329, 20, 5)
                sleep(2)
                break
            else:
                log.info(f"设备：{number},没有找到--听|听|无|妨")

        # 3.首页-任务列表-滑动
        for _ in range(10):
            dm.MoveToEx(901, 241, 5, 5)
            dm.WheelUp()
            sleep(0.1)
            pic_path = dm.Capture(803, 157, 1024, 453, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "宝图任务")
            if retx != -1:
                dm.LeftClick(retx + 803, rety + 157, 5, 5)
                sleep(10)
                break
        # 4.找宝图任务中
        counter = 0
        start_time = time.time()
        while (time.time() - start_time) < 300:
            self.账号信息.SetItem(index, 7, str(counter))
            retx, rety = dm.Find_pic(487, 12, 543, 57, "宝图-决定-2轮.bmp", "000000", 0.8, 0)
            if retx != -1:
                counter = counter + 1
                sleep(30)
                start_time = time.time()
            x, y = dm.Find_pic(268, 5, 357, 81, "首页-活动.bmp", "000000", 0.8, 0)
            if x != -1 and y != -1:
                sleep(20)
                x, y = dm.Find_pic(268, 5, 357, 81, "首页-活动.bmp", "000000", 0.8, 0)
                if x != -1 and y != -1:
                    break

    def mijingxiangyao(self, dm, index):
        # 获取句柄
        hwnd = int(self.账号信息.GetItem(index, 2).GetText())
        print(f"句柄：{hwnd}")
        dm.bind_window(hwnd, 'dx3', 'windows3', 'windows', 1)
        # 1.获取设备编号
        number = get_number_from_title(hwnd)
        self.select_task(dm, number, "秘境降妖")
        sleep(10)
        # 2.首页-秘境降妖
        for _ in range(3):
            sleep(5)
            pic_path = dm.Capture(704,266,997,579, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "秘境降妖")
            if retx != -1:
                dm.LeftClick(retx + 704, rety + 266, 20, 5)
                sleep(2)
                # 3.秘境降妖-选择任务--确定
                for i in range(3):
                    pic_path = dm.Capture(0, 300, 500, 768, f"test{number}.bmp")
                    retx, rety = OCR_get_text(pic_path, "进入")
                    if retx != -1:
                        dm.LeftClick(retx + 0, rety + 300, 20, 5)
                        sleep(2)
                    else:
                        log.info(f"设备：{number},没有找到--秘境降妖-进入")

                    pic_path = dm.Capture(0, 0, 1024, 768, f"test{number}.bmp")
                    retx, rety = OCR_get_text(pic_path, "确定")
                    if retx != -1:
                        dm.LeftClick(retx + 0, rety + 0, 20, 5)
                        sleep(2)
                        break
                    else:
                        log.info(f"设备：{number},没有找到--秘境降妖-确定")
            else:
                log.info(f"设备：{number},没有找到--首页--秘境降妖")
        # 4.秘境降妖--继续挑战
        for _ in range(3):
            sleep(5)
            pic_path = dm.Capture(767, 444, 930, 649, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "继续|挑战")
            if retx != -1:
                dm.LeftClick(retx + 767, rety + 444, 20, 5)
                sleep(5)
                break
            else:
                log.info(f"设备：{number},没有找到--秘境降妖-继续|挑战")
            x, y = dm.Find_pic(767, 444, 930, 649, "秘境降妖-挑战2.bmp", "000000", 0.8, 0)
            if x != -1 and y != -1:
                dm.LeftClick(x, y, 20, 5)
                sleep(5)
                break
        # 5.秘境降妖--战斗
        counter = 0
        start_time = time.time()
        while (time.time() - start_time) < 600:
            pic_path = dm.Capture(771,223,1025,421, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "秘境降妖")
            if retx != -1:
                dm.LeftClick(retx + 771, rety + 223, 5, 5)
                sleep(10)
                pic_path = dm.Capture(706,391,997,585, f"test{number}.bmp")
                retx, rety = OCR_get_text(pic_path, "进入战斗")
                if retx != -1:
                    dm.LeftClick(retx + 706, rety + 391, 5, 5)
                    sleep(5)
                    counter = counter + 1
                    start_time = time.time()
            sleep(10)
            pic_path = dm.Capture(706, 391, 997, 585, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "进入战斗")
            if retx != -1:
                dm.LeftClick(retx + 706, rety + 391, 5, 5)
                sleep(5)
                counter = counter + 1
                start_time = time.time()
            self.账号信息.SetItem(index, 7, str(counter))

            # 判断 战斗失败
            sleep(5)
            x,y = dm.Find_pic(0, 0, 1024, 768, "秘境降妖-失败.bmp", "000000", 0.8, 0)
            if x != -1 and y != -1:
                dm.LeftClick(x, y, 20, 5)
                sleep(5)
                break
            sleep(5)
            pic_path = dm.Capture(0, 0, 1024, 768, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "再接再厉")
            if retx != -1:
                dm.LeftClick(retx, rety, 20, 5)
                sleep(5)
                break

        # 秘境降妖--离开 或 通过
        for _ in range(3):
            pic_path = dm.Capture(787,262,1023,436, f"test{number}.bmp")
            retx, rety = OCR_get_text(pic_path, "离开|通过")
            if retx != -1:
                dm.LeftClick(retx + 787, rety + 262, 5, 5)
                sleep(5)
            x, y = dm.Find_pic(268, 5, 357, 81, "首页-活动.bmp", "000000", 0.8, 0)
            if x != -1 and y != -1:
                break












if __name__ == '__main__':
    app = wx.App()
    frm = Frame()
    frm.Show()
    app.MainLoop()

# pyinstaller -w -D .\main.py -F -p D:\git_project\airtest_homework
